package com.hqhy.like.stock.service.impl;

import com.hqhy.like.common.enumerator.ServiceCode;
import com.hqhy.like.common.ex.ServiceException;
import com.hqhy.like.common.pojo.vo.PageData;
import com.hqhy.like.stock.dao.persist.repository.IStockOutRepository;
import com.hqhy.like.stock.pojo.entity.StockOut;
import com.hqhy.like.stock.pojo.param.StockSearchParam;
import com.hqhy.like.stock.pojo.param.StockOutAddParam;
import com.hqhy.like.stock.pojo.param.StockOutUpdateParam;
import com.hqhy.like.stock.pojo.vo.StockOutListItemVO;
import com.hqhy.like.stock.service.IStockOutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service
public class StockOutServiceImpl implements IStockOutService {

    @Value("${like.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IStockOutRepository stockOutRepository;

    @Override
    public int insert(StockOutAddParam stockOutAddParam) {
        log.debug("开始处理【新增出库】的业务，参数：{}", stockOutAddParam);

        StockOut stockOut = new StockOut();
        BeanUtils.copyProperties(stockOutAddParam, stockOut);
        //转换日期格式
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        stockOut.setStockOutDate(LocalDate.parse(stockOutAddParam.getStockOutDate(), pattern).atStartOfDay());

        int row = stockOutRepository.insert(stockOut);
        if (row != 1) {
            String message = "新增入库订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
        return row;
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始处理【根据ID删除出库订单】的业务，参数：{}", id);
        int rows = stockOutRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除出库数据失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        return rows;
    }

    @Override
    public int updateById(StockOutUpdateParam stockOutUpdateParam) {
        log.debug("开始处理【根据ID更新入库订单】的业务，参数：{}", stockOutUpdateParam);

        StockOut stockOut = new StockOut();
        BeanUtils.copyProperties(stockOutUpdateParam, stockOut);

        //转换日期格式
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        stockOut.setStockOutDate(LocalDate.parse(stockOutUpdateParam.getStockOutDate(), pattern).atStartOfDay());

        int rows = stockOutRepository.updateById(stockOut);
        if (rows != 1) {
            String message = "修改入库数据失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
        return rows;
    }

    @Override
    public PageData<StockOutListItemVO> list(Integer pageNum, Integer size) {
        log.debug("开始处理查询出库情况列表的业务, 页码: {},每页显示: {}", pageNum, size);
        Integer pageSize = size == null ? defaultQueryPageSize : size;
        return stockOutRepository.list(pageNum, pageSize);
    }

    @Override
    public PageData<StockOutListItemVO> listForSearch(StockSearchParam stockInSearchParam) {
        stockInSearchParam.setPageNum(stockInSearchParam.getPageNum() == null ? 1 : stockInSearchParam.getPageNum());
        stockInSearchParam.setPageSize(stockInSearchParam.getPageSize() == null ? defaultQueryPageSize : stockInSearchParam.getPageSize());
        log.debug("开始处理查询入库情况列表的业务,参数: {}", stockInSearchParam);
        return stockOutRepository.listForSearch(stockInSearchParam);
    }

    @Override
    public List<StockOutListItemVO> listForExport(List<Long> ids) {
        log.debug("开始处理导出出库情况列表的业务,参数: {}", ids);
        return stockOutRepository.listStockInByIds(ids);
    }

    @Override
    public StockOutListItemVO getStockOutById(Long id) {
        log.debug("开始处理【根据ID查询入库情况】业务，参数：{}", id);
        StockOutListItemVO stockOutVO = stockOutRepository.getStockOutById(id);
        if (stockOutVO == null) {
            String message = "获取出库情况失败，尝试访问的用户数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return stockOutVO;
    }
}
